package com.woniuxy.java106crm.controller;

import com.woniuxy.java106crm.annotation.RequireMethod;
import com.woniuxy.java106crm.entity.CustomerLinkman;
import com.woniuxy.java106crm.request.ResponseResult;
import com.woniuxy.java106crm.service.CustomerLinkmanService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
@Api(tags = "客户联系人管理模块API文档")
@Slf4j
@RestController
@RequestMapping("/customerLinkman")
public class CustomerLinkmanController {
    @Resource
    private CustomerLinkmanService customerLinkmanService;

    //查询客户对应的联系人信息
    @PreAuthorize("hasAuthority('客户信息管理')")
    @RequireMethod("查询客户对应的联系人信息")
    @GetMapping("/findAllLinkmanByCid/{cid}")
    public ResponseResult<List<CustomerLinkman>> findAllLinkmanByCid(@PathVariable("cid")String cid){
        List<CustomerLinkman> linkmen = customerLinkmanService.findAllLinkmanByCid(cid);
        return new ResponseResult<>(ResponseResult.OK,linkmen);
    }

    //添加客户联系人
    @PreAuthorize("hasAuthority('客户信息管理')")
    @RequireMethod("添加客户联系")
    @PostMapping("/addLinkman")
    public ResponseResult<Boolean> addLinkman(@RequestBody CustomerLinkman linkman){
        return new ResponseResult<>(ResponseResult.OK,customerLinkmanService.addLinkman(linkman));
    }

    @PreAuthorize("hasAuthority('客户信息管理')")
    @RequireMethod("删除客户联系人")
    @DeleteMapping("/deleteById/{id}")
    public ResponseResult<Boolean> deleteById(@PathVariable("id") Integer id){
        return new ResponseResult<>(ResponseResult.OK,customerLinkmanService.deleteById(id));
    }

    @PreAuthorize("hasAuthority('客户信息管理')")
    @RequireMethod("编辑客户联系")
    @PostMapping("/updateById")
    public ResponseResult<Boolean> updateById(@RequestBody CustomerLinkman linkman){
        return new ResponseResult<>(ResponseResult.OK,customerLinkmanService.updateById(linkman));
    }

    @PreAuthorize("hasAuthority('客户信息管理')")
    @RequireMethod("批量删除客户联系人")
    @DeleteMapping("/batchedDelByIds/{ids}")
    public ResponseResult<Boolean> batchedDelByIds(@PathVariable("ids")String ids){
//        log.debug("{}",ids);
        customerLinkmanService.batchedDelByIds(ids);
        return new ResponseResult<>(ResponseResult.OK,true);
    }
}
